/*
 * Albareth - an action roleplaying game.
 * Copyright (C) 2008 Jens Anuth
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 *   02110-1301, USA.
 * 
 * $Id: MessageWindow.java 38 2008-05-07 20:00:46Z jens464 $
*/
package albareth.frontend.slick.ui;

import java.util.Iterator;
import java.util.LinkedList;

import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;

import albareth.frontend.slick.state.InGameState;

public class MessageWindow extends UIElement
{
    public static final Color COLOR_EVENT = new Color(255, 255, 160);
    public static final Color COLOR_WARN = new Color(255, 200, 200);
    public static final Color COLOR_INFO = Color.white;

    //Font _font = SlickResources.GAME_FONT;
    private LinkedList _messages = new LinkedList();
    //private int _maxLines;
    //private int _lineHeight;
    //Container _labelContainer;
    
    private class Message
    {
        String text = "";
        Color textColor = null;
    }
    
    public MessageWindow()
    {
        setId(InGameState.COMP_MESSAGES);
    }
    
    public void clear()
    {
        _messages = new LinkedList();
    }
    
    public final void addMessage(String text)
    {
        addMessage(text, Color.white);
    }
    
    public void addMessage(String text, Color color)
    {
        //System.err.println(_font);
        text = text.toUpperCase();
        Object[] lines = UIToolkit.breakLines(text, _componentWidth, getFont());
        for (int i = 0; i < lines.length; i++) {
            addSingleLine((String)lines[i], color);
        }
    }
    
    private void addSingleLine(String text, Color color)
    {
        //purge:
        if (_componentHeight > 0)
        {
            int maxLines = _componentHeight / getFont().getLineHeight();
            if (!_messages.isEmpty() && _messages.size() >= maxLines) {
                _messages.removeFirst();
            }
        }

        Message message = new Message();
        message.text = text;
        message.textColor = color;
        _messages.addLast(message);


        
    }
    

    public void render(Graphics g, GameContainer container,
                       int screenX, int screenY) throws SlickException
    {
        //g.setColor(Color.cyan);
        //g.drawRect(screenX, screenY, componentWidth, componentHeight);
        
        int currMsg = 0;
        for (Iterator it = _messages.iterator(); it.hasNext(); )
        {

            Message message = (Message)it.next();
            int y = currMsg * getFont().getLineHeight();
            //g.setColor(Color.darkGray);
            //g.fillRect(0 + screenX, y + screenY, _font.getWidth(message.text), _lineHeight);
            g.setColor(message.textColor);
            g.drawString(message.text, 0 + screenX, y + screenY);
            
            currMsg++;
        }
    }
    

}
